package com.ktjiaoyu.utils.fastjson2;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.PropertyPreFilter;

import java.util.Set;

public class FastJson2SensitivePropFilter implements PropertyPreFilter {
    private Set<String> excludes;

    //通过构造器注入需要排除的属性集合
    public FastJson2SensitivePropFilter(Set<String> excludes) {
        this.excludes = excludes;
    }

    //处理字段，返回true表示字段不需要处理，返回false表示字段需要处理
    @Override
    public boolean process(JSONWriter jsonWriter, Object obj, String propertyName) {
        if (excludes ==  null){
            //没有需要排除的属性
            return true;
        }
        //不包含再excludes中的属性才需要序列化
        boolean isNeed = !excludes.contains(propertyName);
        return isNeed;
    }
}
